package com.wy.controller;

import com.wy.pojo.CountHistory;
import com.wy.service.WelcomeService;
import com.wy.service.impl.CountHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("welcome")
public class WelcomeController {

    @Autowired
    WelcomeService service;
    @Autowired
    private CountHistoryService countHistoryService;
    /**
     * 管理员获取首页数量
     * @return 数量信息
     */
    @GetMapping("getAllCount.do")
    public Map<String, Integer> getAllCount() {
        return service.getAllCount();
    }

    /**
     * 学生获取首页数量
     * @param sid 学生id
     * @return 数量信息
     */
    @GetMapping("student/getAllCountByStudent.do")
    public Map<String, Integer> getAllCountByStudent(Integer sid) {
        return service.getAllCountByStudent(sid);
    }

    /**
     * 教师获取首页数量
     * @param tid 教师id
     * @return 数量信息
     */
    @GetMapping("teacher/getAllCountByTeacher.do")
    public Map<String, Integer> getAllCountByTeacher(Integer tid) {
        return service.getAllCountByTeacher(tid);
    }
    @GetMapping("getHistoryCount.do")
    public Map<String, Object> getHistoryCount(
            @RequestParam(value = "startDate", required = false) String startDateStr,
            @RequestParam(value = "endDate", required = false) String endDateStr) {
        // 默认日期范围为最近两个月
        if (startDateStr == null || endDateStr == null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date today = new Date();

            // 计算两个月前的日期
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(today);
            calendar.add(Calendar.MONTH, -2);  // 设置为2个月前
            Date twoMonthsAgo = calendar.getTime();

            // 格式化日期为字符串
            startDateStr = sdf.format(twoMonthsAgo);
            endDateStr = sdf.format(today);

            // 打印日期用于调试
            System.out.println("Start Date: " + startDateStr); // 打印两个月前的日期
            System.out.println("End Date: " + endDateStr); // 打印当前日期
        }

        // 获取历史数据
        List<CountHistory> historyList = countHistoryService.getCountHistoryByDateRange(startDateStr, endDateStr);

        // 创建返回的响应对象
        List<Map<String, Object>> responseData = new ArrayList<>();
        for (CountHistory history : historyList) {
            Map<String, Object> dataEntry = new HashMap<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            dataEntry.put("time", sdf.format(history.getDate())); // 转换为字符串格式
            dataEntry.put("studentCount", history.getStudentCount()); // 学生数量
            dataEntry.put("teacherCount", history.getTeacherCount()); // 教师数量
            dataEntry.put("openCourseCount", history.getOpenCourseCount()); // 开课数量
            dataEntry.put("approvalUntreatedCount", history.getApprovalUntreatedCount()); // 审批未处理数量
            responseData.add(dataEntry);
        }

        Map<String, Object> response = new HashMap<>();
        response.put("data", responseData); // 将历史数据放入 "data" 字段中

        // 打印日志以检查返回的数据
        System.out.println("返回的历史数据: " + response);

        return response; // 返回响应对象
    }

}
